﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PowerTriggers.ServiceReference;
using System.Management;

namespace PowerTriggers
{
    internal partial class SelectServerName : Form
    {
        private SelectServerName()
        {
            InitializeComponent();
        }

        public SelectServerName(string serverName)
            : this()
        {
            this.ServerName = serverName;
            radioButtonRemote.Checked = !string.IsNullOrEmpty(serverName);
            textBox1.Text = serverName ?? string.Empty;
        }

        internal string ServerName
        {
            get;
            set;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (radioButtonLocal.Checked)
                {
                    this.ServerName = null;
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    string serverName = textBox1.Text.ToLower().Trim();
                    if (serverName.Length == 0)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show(this, "You must enter a computer name");
                        if (textBox1.CanFocus)
                        {
                            textBox1.Focus();
                        }
                    }
                    else
                    {
                        bool success;
                        try
                        {
                            if (WcfClientFactory.DoesWindowsServiceExist(serverName))
                            {
                                WcfClientFactory.EnsureWindowsServiceIsRunning(serverName);

                                using (var client = WcfClientFactory.OpenWcfClient(serverName))
                                {
                                    success = true;
                                }
                            }
                            else
                            {
                                success = false;
                                Cursor.Current = Cursors.Default;
                                MessageBox.Show(this, "Cannot find a Power Triggers Service running on computer " + serverName, "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        catch (System.Exception ex)
                        {
                            success = false;
                            Cursor.Current = Cursors.Default;
                            MessageBox.Show(this, "Error connecting to server " + serverName + ": " + ex.Message, "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        if (success)
                        {
                            this.ServerName = serverName;
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                        else
                        {
                            if (textBox1.CanFocus)
                            {
                                textBox1.Focus();
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(this, "Error: " + ex.Message, "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            panelServerName.Enabled = radioButtonRemote.Checked;
            if (panelServerName.Enabled && textBox1.CanFocus)
            {
                textBox1.Focus();
            }
        }
    }
}
